/**
 * Created by zuodahua on 2016/10/23.
 *  一些工具类
 */

/**
 * 设置指定的cookie
 * @param name
 * @param value
 */
function setCookie(name, value){
    //var cookies = document.cookie.split(";");
    //var isContainsName = false;
    //for(var index = 0; index < cookies.length; index++){
    //    var cookie = cookies[index];
    //    if (cookie.trim() === ""){
    //        cookies.splice(index, 1);
    //        index--;
    //    }else {
    //        cookie = cookie.split("=");
    //        var cookieName = cookie[0].trim();
    //        if (cookieName === name.trim()){
    //            cookie = name + "=" + value;
    //            document.cookie = cookie;
    //            isContainsName = true;
    //            return;
    //        }
    //    }
    //}
    //if (isContainsName === false){
    //    // 没有找到对应的key，直接添加相应的cookie
    //    var cookie = name + "=" + value;
    //    document.cookie = cookie;
    //}
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

/**
 * 在cookie中获取指定的name的cookie
 * @param name
 * @returns {*}
 */
function getCookie(name){
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++){
        var cookie = cookies[i].split("=");
        var cookieName = cookie[0].trim();
        if (cookieName === name){
            return cookie[1].trim();
        }
    }
    return null;
}

function delCookie(name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000); //删除一个cookie，就是将其过期时间设定为一个过去的时间
    document.cookie = name + "=删除" + "; expires=" + date.toUTCString();
    //document.cookie = " " + name + "=删除" + "; expires=" + date.toGMTString();
}

/**
 * 获取指定年月的天数，比如7月有31天，9月有30天
 * @param year
 * @param month
 * @returns {number}
 */
function getMonthNumDates(year, month){
    return (new Date(year, month, 0)).getDate();
}
function u2str(text){ // transform text in utf8 format to string
    return unescape(text.replace(/&#/g,'%u').replace(/;/g,''));
}
function changetime(time){
    var da = new Date(time);
    var year = da.getFullYear();
    if(da.getMonth()+1 <10){
        var month = "0" + (da.getMonth()+1);
    }else{
        month = da.getMonth()+1;
    }
    if(da.getDate()<10){
        var date = "0" + da.getDate();
    }else{
        date = da.getDate();
    }
    return([year,month,date].join('-'));
}
